package com.bm.user.service.impl;

import com.bm.common.RabbitMqConstants;
import com.bm.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public void sendCode(String mail,String msg) {
        rabbitTemplate.convertAndSend(
                RabbitMqConstants.MQ_FANOUT_EXCHANGE,
                "routingKey.fanout",msg);
    }

    @Override
    public void sendTopic(String msg) {

        rabbitTemplate.setConfirmCallback((c,ack,m)->{
            log.debug("c.getReturned():",c.getReturned());
            log.debug("ack:",ack);
            log.debug("m:",m);
            if(ack){
                log.debug("ack：",ack);
            }

        });
        rabbitTemplate.setReturnsCallback(r->{
            log.debug("返回文字:{}",r.getReplyText());
            log.debug("返回code:{}",r.getReplyCode());
            log.debug("返回Exchange:{}",r.getExchange());
            log.debug("返回RoutingKey:{}",r.getRoutingKey());
        });

        rabbitTemplate.convertAndSend(
                RabbitMqConstants.MQ_TOPIC_EXCHANGE,
                "topic.mail",msg);
    }

    @Override
    public void delayDelay(String msg, int delayTime) {
        rabbitTemplate.convertAndSend(
                RabbitMqConstants.MQ_FANOUT_EXCHANGE,
                RabbitMqConstants.MQ_NORMAL_ROUTING_KEY,
                msg,
                process->{
                    process.getMessageProperties().setExpiration(String.valueOf(delayTime));
                    return process;
                }
        );
    }
}
